let userInfo;
//查询会员状态
$(function (){
    $.ajax({
        headers: {
            "token": localStorage.getItem("token")
        },
        type: 'get',
        url: '/devs/user',
        async: false,
        success: function (result) {
            userInfo = result.data;
        }
    })
    if (userInfo!=null) {
        memberStatus()
    }else {
        let str=`<div class="vip-open">
                    <span>Sorry!您还没有登录，请先登录!<a class="buy-btn vip" href="../html/login.html">登录</a></span>
                 </div>`;
        document.getElementById("vip").innerHTML = str;
    }
    $.ajax({
        type:'get',
        url:`/devs/member/price`,
        success:function (result){
            let str = `<p class="buyright-middle01">原价&nbsp;¥<span style="text-decoration: line-through;">${result.data.oldPrice}</span></p>
                        <p class="buyright-middle02">现价&nbsp;¥<a style="font-size: 46px;" id="newPrice">${result.data.newPrice}</a></p>`
            document.getElementById("price").innerHTML = str;
        }
    })

})

function memberStatus(){
    let id = userInfo.id
    $.ajax({
        type: 'get',
        url:`/devs/member/findByUserId/${id}`,
        success:function (result){
            let data = result.data;
            let time = data.expirationTime;
            let time2 = data.openTime;
            let openTime = parseInt(time2.slice(0,4),10).toString()+"年"+parseInt(time2.slice(5,7),10).toString()+"月"+parseInt(time2.slice(8,10),10).toString()+"日";
            let expirationTime = parseInt(time.slice(0,4),10).toString()+"年"+parseInt(time.slice(5,7),10).toString()+"月"+parseInt(time.slice(8,10),10).toString()+"日";
            if (result.code == 500){
                let str=`<div class="vip-open">
                            <span>Sorry!您还不是我们的会员!👉<a onclick="openMember()" class="buy-btn vip">开通会员</a></span>
                        </div>`;
                document.getElementById("vip").innerHTML=str;
            }else if (result.code==505){
                let str=`<table class="vip-table" id="vip-table">
                            <thead>
                            <tr>
                                <th>状态</th>
                                <th>开通时间</th>
                                <th>截止时间</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            <tr>
                                <td><span class="vip-begin-time">已过期</span></td>
                                <td><span class="vip-begin-time">${openTime}</span></td>
                                <td><span class="vip-end-time">${expirationTime}</span></td>
                                <td><a onclick="reopen()" class="buy-btn vip">重新开通会员</a></td>
                            </tr>
                            </tbody>
                        </table>`;
                document.getElementById("vip").innerHTML=str;
            }else {
                let str = `<table class="vip-table" id="vip-table">
                                <thead>
                                <tr>
                                    <th>开通时间</th>
                                    <th>截止时间</th>
                                    <th>操作</th>
                                </tr>
                                </thead>
                                <tbody>
                                    <tr>
                                        <td><span class="vip-begin-time">${openTime}</span></td>
                                        <td><span class="vip-end-time">${expirationTime}</span></td>
                                        <td><a onclick="renew()" class="buy-btn vip" >续费会员</a></td>
                                    </tr>
                                </tbody>
                            </table>`;
                document.getElementById("vip").innerHTML = str;
            }
        }
    })
}
//续费会员
function renew(){
    let price = document.getElementById("newPrice").textContent;
    let id = userInfo.id;
    $.ajax({
        type:'get',
        url:`/devs/alipay/payVip`,
        data:{userId:id,secId:1,price:price},
        success:function (alipayRedirectUrl){
            let div = document.createElement("div");

            // 将后台返回的支付宝支付页面的HTML代码赋值给div的innerHTML
            div.innerHTML = alipayRedirectUrl;

            // 将div添加到页面的body元素中
            document.body.appendChild(div);

            // 获取form元素
            let form = document.forms["punchout_form"];

            // 提交form表单，模拟用户点击操作
            form.submit();

        }
    })
}
//开通会员
function openMember(){
    let price = document.getElementById("newPrice").textContent;
    let id = userInfo.id
    $.ajax({
        type:'get',
        url:`/devs/alipay/payVip`,
        data:{userId:id,secId:0,price:price},
        success:function (alipayRedirectUrl){
            let div = document.createElement("div");

            // 将后台返回的支付宝支付页面的HTML代码赋值给div的innerHTML
            div.innerHTML = alipayRedirectUrl;

            // 将div添加到页面的body元素中
            document.body.appendChild(div);

            // 获取form元素
            let form = document.forms["punchout_form"];

            // 提交form表单，模拟用户点击操作
            form.submit();

        }
    })
}

//过期重新开通
function reopen(){
    let price = document.getElementById("newPrice").textContent;
    let id = userInfo.id
    $.ajax({
        type:'get',
        url:`/devs/alipay/payVip`,
        data:{userId:id,secId:2,price:price},
        success:function (alipayRedirectUrl){
            let div = document.createElement("div");

            // 将后台返回的支付宝支付页面的HTML代码赋值给div的innerHTML
            div.innerHTML = alipayRedirectUrl;

            // 将div添加到页面的body元素中
            document.body.appendChild(div);

            // 获取form元素
            let form = document.forms["punchout_form"];

            // 提交form表单，模拟用户点击操作
            form.submit();

        }
    })
}